<template>
  <div class='article-container'>
    <!-- 表单区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-form label-width="80px">

        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <!-- el-radio来说道，label决定用户选中的值 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 添加自定义事件的监听 -->
        <!-- <my-channel @input="hInput"></my-channel> -->
        <my-channel v-model="form.channel_id"></my-channel>
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.dateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="hQuery" :loading="isLoading">查询</el-button>
        </el-form-item>
      </el-form>

    </el-card>

    <!-- 表格区域 -->
    <el-card class="box-card" style="margin-top:20px;">
      <div slot="header" class="clearfix">
        <span>查询到{{total_count}}条结果</span>
      </div>
      <el-table
        v-loading="isLoading"
        :data="articles"
        style="width: 100%">
        <el-table-column
          label="封面"
          width="180">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" />
          </template>
        </el-table-column>

        <el-table-column
          prop="title"
          label="标题"
          width="180">
        </el-table-column>

        <el-table-column
          label="状态">
          <template slot-scope="scope">
            <el-tag :type='scope.row.status | fStatusType'>{{scope.row.status | fStatusText}}</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="pubdate"
          label="发布时间">
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              size="mini"
              :circle="true"
              icon="el-icon-edit"
              @click="hEdit(scope.row)"></el-button>
            <el-button
              size="mini"
              circle
              type="danger"
              icon="el-icon-delete"
              @click="hDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="10"
        @current-change="hCurrentChange"
        :total="total_count">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import MyChannel from '../../components/MyChannel.vue'
export default {
  name: 'articles',
  components: {
    MyChannel: MyChannel
  },
  data () {
    return {
      isLoading: false,
      form: {
        channel_id: null,
        status: null,
        dateRange: ''
      },
      page: 1,
      total_count: 0,
      articles: []
    }
  },
  created () {
    // 发请求
    this.loadArticles()
  },
  filters: {
    fStatusType (status) {
      const texts = {
        0: 'info',
        1: 'normal',
        2: 'success',
        3: 'warning',
        4: 'danger'
      }
      return texts[status]
    },
    fStatusText (status) {
      const texts = {
        0: '草稿',
        1: '待审核',
        2: '审核通过',
        3: '审核失败',
        4: '已删除'
      }
      return texts[status]
    }
  },
  methods: {
    // hInput (channelId) {
    //   console.log('父组件收到了子组件传递的数据', channelId)
    //   this.form.channel_id = channelId
    // },
    hQuery () {
      this.loadArticles()
    },
    buildParams () {
      const { dateRange, status } = this.form
      // eslint-disable-next-line camelcase
      const channel_id = this.form.channel_id === '' ? null : this.form.channel_id

      const params = {
        channel_id: channel_id, // 频道id
        page: this.page, // 页码
        status: status // 状态
      }

      if (dateRange) {
        params.begin_pubdate = dateRange[0]
        params.end_pubdate = dateRange[1]
      }

      // 返回一个 条件 对象
      return params
    },
    async loadArticles () {
      // 1. 获取查询条件
      const params = this.buildParams()
      this.isLoading = true
      try {
        // 2. 带参数发请求
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles',
          params: params
        })
        // 3. 保存数据到数据项
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        // 发生错误，清空数据
        this.articles = []
      }
      this.isLoading = false
    },
    hCurrentChange (curPage) {
      // curPage会自动传入，表示当前的页码
      console.log('现在要显示', curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadArticles()
    },
    // 编辑功能
    hEdit (row) {
      // 跳转
      const id = row.id.toString()
      this.$router.push('/edit/' + id)
    },
    // 封装删除请求
    async doDelete (id) {
      try {
        const res = await this.$axios({
          method: 'DELETE',
          url: '/mp/v1_0/articles/' + id
        })
        console.log(res)
        this.loadArticles()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 删除功能
    hDelete (row) {
      this.$confirm('你确定要删除吗？').then(() => {
        console.log('你要删除的', row.id.toString())
        const id = row.id
        this.doDelete(id)
      }).catch(() => {
        console.log('删除取消了')
      })
    }
  }
}
</script>
